<?php
declare(strict_types=1);
namespace Service\VenderClient\IdCardAuth;

use Service\BaseService;
use Service\Code\Normal;
use Service\ExtraService\TaskWorker;

class IdCardAuthClient extends BaseService
{
    protected $config;
    public function __construct( array $apiBody = [] )
    {
        $this->config = $apiBody;
    }

    public function check($param)
    {
        //请求二要素
        $url = 'https://api.253.com/open/idcard/id-card-auth';
        $data = [
            'appId'  => $this->config['appId'],
            'appKey' => $this->config['appKey'],
            'name'   => $param['true_name'],
            'idNum'  => $param['idcard']
        ];

        $parameter = [
            [
                'tag'       => 'id_card_auth',
                'url'       => $url,
                'data'      => $data,
                'method'    => 'post',
                'format'    => 'x-www-form-urlencoded',
                'header'    => [],
            ],
        ];
        $resTask    = TaskWorker::instance()->addTask($parameter);
        $resTask    = current($resTask);
        $response   = @json_decode($resTask['response']['result'],true);

        if($response['code'] != 200000){
            return Normal::returnCode(Normal::FAIL,'实名认证失败'.$response['message']);
        }

        if($response['data']['result'] != '01'){
            return Normal::returnCode(Normal::FAIL,'实名认证信息'.$response['data']['remark'].',请确认信息');
        }

        return Normal::returnCode(Normal::SUC,'认证成功');
    }

}